#include <cstdio>
#include <unistd.h>
#include <cassert>
#include <cstdlib>
#include <cstdarg>
#include <time.h>

using namespace std;

#define DEBUG 1
#define NOTICE 2
#define WARNING 3
#define FATAL 4

const char* log_level[] = {"DEBUG", "NOTICE","WARNING", "FATAL"};

void Log(int level, const char* format,...)
{
    assert(DEBUG <= level && level <= FATAL);
    char* name = getenv("USER");
    char logInfo[1024];
    va_list ap;
    va_start(ap, format);
    vsnprintf(logInfo,sizeof(logInfo) - 1, format, ap);
    va_end(ap);
    FILE* out = (level == FATAL) ? stderr : stdout;
    fprintf(out, "%s | %u | %s | %s\n",
            log_level[level], (unsigned)time(nullptr),
            name == nullptr ? "unknow" : name, logInfo);
}
